from concurrent import futures
from concurrent.futures import ThreadPoolExecutor
from typing import List


def thread_pool_list(funName, paramsList: List):
    """
        funName: 需要并发执行的函数名称
        paramsList： 根据列表长度启动，对应的线程池启动对应的线程数
    """
    with ThreadPoolExecutor(max_workers=len(paramsList)) as pool:
        threads = [pool.submit(funName, param) for param in paramsList]
        futures.wait(threads, return_when=futures.FIRST_EXCEPTION)
        for t in threads:
            t.result()


def thread_pool_num(funName, num, ifNum=False):
    """
    :param funName: 需要并发执行的函数名称
    :param num: 并发的线程池的线程数
    :param ifNum: 并发的函数是否含有入参
    :return:
    """
    with ThreadPoolExecutor(max_workers=num) as pool:
        if ifNum:
            threads = [pool.submit(funName, i) for i in range(num)]
            futures.wait(threads, return_when=futures.FIRST_EXCEPTION)
            for t in threads:
                t.result()
        else:
            threads = [pool.submit(funName) for i in range(num)]
            futures.wait(threads, return_when=futures.FIRST_EXCEPTION)
            for t in threads:
                t.result()